#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::Widget)
{
    ui->setupUi(this);
  //  ui->treeWidget->setColumnCount(2);
    QTreeWidgetItem *headerItem=new QTreeWidgetItem();
    headerItem->setText(0,"动物园");
   // headerItem->setText(1,"宠物园");
    ui->treeWidget->setHeaderItem(headerItem);

    QTreeWidgetItem*item1=new QTreeWidgetItem();
    item1->setText(0,"老虎");
    //item1->setText(1,"狗");
    ui->treeWidget->addTopLevelItem(item1);

    QTreeWidgetItem*item2=new QTreeWidgetItem();
    item2->setText(0,"蛇");
    //item1->setText(1,"猫");
    ui->treeWidget->addTopLevelItem(item2);

    QTreeWidgetItem*item3=new QTreeWidgetItem();
    item3->setText(0,"狗");
    ui->treeWidget->addTopLevelItem(item3);



}

Widget::~Widget()
{
    delete ui;
}


void Widget::on_pushButton_clicked()
{
    if(!ui->lineEdit->text().isEmpty()){
        QTreeWidgetItem*items=new QTreeWidgetItem();
        items->setText(0,ui->lineEdit->text());
        ui->treeWidget->addTopLevelItem(items);
    }
}

void Widget::on_pushButton_2_clicked()
{
    if(!ui->lineEdit->text().isEmpty()){
        QTreeWidgetItem* currentItem=ui->treeWidget->currentItem();
        if(currentItem==NULL){
            return ;
        }
        QTreeWidgetItem*newItem=new QTreeWidgetItem();
        newItem->setText(0,ui->lineEdit->text());
        currentItem->addChild(newItem);
        currentItem->setExpanded(true);

    }
}

void Widget::on_pushButton_3_clicked()
{
     QTreeWidgetItem* currentItem=ui->treeWidget->currentItem();
     if(currentItem==NULL){
         return ;
     }
     QTreeWidgetItem* parent=currentItem->parent();
     if(parent==NULL)
     {
         int index=ui->treeWidget->indexOfTopLevelItem(currentItem);
         ui->treeWidget->takeTopLevelItem(index);
     }
     else{
         parent->removeChild(currentItem);
     }
}
